﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Modles.DTO;
using Modles.Models;

namespace BilbilDAL
{
    public class VideoHistoryDAL
    {
        /// <summary>
        /// 获取观看已视频的历史记录
        /// </summary>
        /// <param name="UserInfoId"></param>
        /// <returns></returns>
        public List<VideoInfo_DTO> GetVideoHistory(int UserInfoId)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                IQueryable<VideoInfo_DTO> res = from vh in com.VideoHistories
                                                 where vh.UserInfoId == UserInfoId
                                                 join vi in com.VideoInfos on vh.VideoInfoId equals vi.Id
                                                 join v in com.Videos on vi.VideoId equals v.Id
                                                 join img in com.Images on vi.ImageId equals img.Id
                                                 join vt in com.VideoTypes on vi.VideoTypeId equals vt.Id
                                                 join u in com.UserInfos on vi.UserInfoId equals u.Id
                                                 select new VideoInfo_DTO
                                                 {
                                                     Id = vi.Id,
                                                     VideoId = vi.VideoId,
                                                     ImageId = vi.ImageId,
                                                     VideoTypeId = vi.VideoTypeId,
                                                     UserInfoId = vi.UserInfoId,
                                                     Title = vi.Title,
                                                     Briefintroduction = vi.Briefintroduction,
                                                     Label = vi.Label,
                                                     Time = vi.Time,
                                                     Like = vi.Like,
                                                     Views = vi.Views,
                                                     VideoAddress = v.Address,
                                                     ImageAddress = img.Address,
                                                     VideoType = vt.Type,
                                                     UserName = u.Name,
                                                     UserAvatar = u.Avatar
                                                 };
                return res.ToList();
            }
        }
        /// <summary>
        /// 获取今天观看过的视频记录
        /// </summary>
        /// <param name="UserInfoId"></param>
        /// <returns></returns>
        public List<VideoInfo_DTO> GetVideoHistoryToday(int UserInfoId)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                DateTime todayStart = DateTime.Today; // 今天 00:00:00
                DateTime tomorrowStart = todayStart.AddDays(1); // 明天 00:00:00
                IQueryable<VideoInfo_DTO> res = from vh in com.VideoHistories
                                                where vh.UserInfoId == UserInfoId && vh.DateTime >= todayStart && vh.DateTime < tomorrowStart
                                                join vi in com.VideoInfos on vh.VideoInfoId equals vi.Id
                                                join v in com.Videos on vi.VideoId equals v.Id
                                                join img in com.Images on vi.ImageId equals img.Id
                                                join vt in com.VideoTypes on vi.VideoTypeId equals vt.Id
                                                join u in com.UserInfos on vi.UserInfoId equals u.Id
                                                select new VideoInfo_DTO
                                                {
                                                    Id = vi.Id,
                                                    VideoId = vi.VideoId,
                                                    ImageId = vi.ImageId,
                                                    VideoTypeId = vi.VideoTypeId,
                                                    UserInfoId = vi.UserInfoId,
                                                    Title = vi.Title,
                                                    Briefintroduction = vi.Briefintroduction,
                                                    Label = vi.Label,
                                                    Time = vi.Time,
                                                    Like = vi.Like,
                                                    Views = vi.Views,
                                                    VideoAddress = v.Address,
                                                    ImageAddress = img.Address,
                                                    VideoType = vt.Type,
                                                    UserName = u.Name,
                                                    UserAvatar = u.Avatar
                                                };
                return res.ToList();
            }
        }
        /// <summary>
        /// 获取昨天观看过的视频记录
        /// </summary>
        /// <param name="UserInfoId"></param>
        /// <returns></returns>
        public List<VideoInfo_DTO> GetVideoHistoryYesterday(int UserInfoId)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                DateTime datetime = DateTime.Today.AddDays(-1);
                IQueryable<VideoInfo_DTO> res = from vh in com.VideoHistories
                                                where vh.UserInfoId == UserInfoId && EF.Functions.DateDiffDay(vh.DateTime, datetime) == 0
                                                join vi in com.VideoInfos on vh.VideoInfoId equals vi.Id
                                                join v in com.Videos on vi.VideoId equals v.Id
                                                join img in com.Images on vi.ImageId equals img.Id
                                                join vt in com.VideoTypes on vi.VideoTypeId equals vt.Id
                                                join u in com.UserInfos on vi.UserInfoId equals u.Id
                                                select new VideoInfo_DTO
                                                {
                                                    Id = vi.Id,
                                                    VideoId = vi.VideoId,
                                                    ImageId = vi.ImageId,
                                                    VideoTypeId = vi.VideoTypeId,
                                                    UserInfoId = vi.UserInfoId,
                                                    Title = vi.Title,
                                                    Briefintroduction = vi.Briefintroduction,
                                                    Label = vi.Label,
                                                    Time = vi.Time,
                                                    Like = vi.Like,
                                                    Views = vi.Views,
                                                    VideoAddress = v.Address,
                                                    ImageAddress = img.Address,
                                                    VideoType = vt.Type,
                                                    UserName = u.Name,
                                                    UserAvatar = u.Avatar
                                                };
                return res.ToList();
            }
        }
        /// <summary>
        /// 获取更久之前的视频记录
        /// </summary>
        /// <param name="UserInfoId"></param>
        /// <returns></returns>
        public List<VideoInfo_DTO> GetVideoHistoryALongTimeAgo(int UserInfoId)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                DateTime dateTime = DateTime.Today.AddDays(-1);
                IQueryable<VideoInfo_DTO> res = from vh in com.VideoHistories
                                                where vh.UserInfoId == UserInfoId && vh.DateTime < dateTime
                                                join vi in com.VideoInfos on vh.VideoInfoId equals vi.Id
                                                join v in com.Videos on vi.VideoId equals v.Id
                                                join img in com.Images on vi.ImageId equals img.Id
                                                join vt in com.VideoTypes on vi.VideoTypeId equals vt.Id
                                                join u in com.UserInfos on vi.UserInfoId equals u.Id
                                                select new VideoInfo_DTO
                                                {
                                                    Id = vi.Id,
                                                    VideoId = vi.VideoId,
                                                    ImageId = vi.ImageId,
                                                    VideoTypeId = vi.VideoTypeId,
                                                    UserInfoId = vi.UserInfoId,
                                                    Title = vi.Title,
                                                    Briefintroduction = vi.Briefintroduction,
                                                    Label = vi.Label,
                                                    Time = vi.Time,
                                                    Like = vi.Like,
                                                    Views = vi.Views,
                                                    VideoAddress = v.Address,
                                                    ImageAddress = img.Address,
                                                    VideoType = vt.Type,
                                                    UserName = u.Name,
                                                    UserAvatar = u.Avatar
                                                };
                return res.ToList();
            }
        }

    }
}
